/*******************************************************************************
 *
 ******************************************************************************/

$(function () {

    // 记录所有滑动器
    var wbps_arraySwiper = {};
    var firstAnimate = true;
    // 初始化滑动器
    $(".wbps_slider").each(function () {
        //
        var controller = $(this).attr("controller");
        if (controller) {
            //
            var this_swiper = $(this)
            $(this).addClass(controller);
            //
            var objectController = "." + controller;
            var objectDirection = (null != $(this).attr("direction") ? $(this).attr("direction") : 'vertical');
            var initIndexSlide = (null != $(this).attr("initIndex") ? $(this).attr("initIndex") : 0);
            wbps_arraySwiper[controller] = new Swiper(objectController, {
                direction: objectDirection,
                pagination: '.swiper-pagination[controller="' + controller + '_pagination"]',
                paginationClickable: true,
                initialSlide: initIndexSlide,
                onSlideChangeStart: function (swiper) {
                    // 同步菜单高亮
                    var sliderController = swiper.container.attr("controller");
                    $(".wbps_slider_menu").each(function () {
                        if (sliderController == $(this).attr('controller')) {
                            $(this).children('img').each(function (index) {
                                if (index == swiper.activeIndex) {
                                    wbps_updateMenuStatu($(this));
                                };
                            });
                        }
                    });
                    // 滑动强制重做动画
                    $("." + sliderController + ">.swiper-wrapper>.swiper-slide>img").each(function () {
                        if ($(this).hasClass("autorun")) {
                            var valClass = $(this).attr("class");
                            $(this).removeClass(valClass);
                            $(this).width();
                            $(this).addClass(valClass);
                        }
                    });

                    // 户型详情和总览的切换处理
                    wbht_syncUpdateSlider(swiper.container, swiper.activeIndex);
                }
                ,
                onTransitionStart: function (swiper) {
                    if (swiper.previousIndex == swiper.activeIndex) {
                        // 回溯上级滑动器，如果有则自动滑动到下一个兄弟
                        var slideObjectController = $("." + swiper.container.attr("controller"));
                        var parentController = slideObjectController.parent().parent().parent().attr("controller");
                        if ('prev' == swiper.swipeDirection) {

                            $('.wbps_slider').each(function () {
                                if ($(this).attr('controller') == 'main_control') {
                                    var main_control_initindex = $(this).attr('initindex');

                                    // 户型鉴赏:特殊处理;两种情况: 总览在上、总览在下
                                    // 总览在下
                                    if (main_control_initindex == '1') {

                                        while (parentController) {
                                            if (wbps_arraySwiper[parentController].isBeginning) {
                                                // 到达父容器的最前或最后，追溯更上一级父容器
                                                parentController = $('.' + parentController).parent().parent().parent().attr("controller");

                                                if (slideObjectController.parent().parent().parent().hasClass('wbht_slider_detail')) {
                                                    var main_control = 'main_control';
                                                    wbps_arraySwiper[main_control].slidePrev();
                                                }
                                            } else {
                                                wbps_arraySwiper[parentController].slidePrev();
                                                parentController = null;
                                            }
                                        }
                                    }
                                    // 总览在上
                                    else {

                                        if (slideObjectController.parent().parent().parent().hasClass('wbht_slider_detail')) {
                                            var main_control = 'main_control';
                                            if (swiper.isBeginning) {
                                                wbps_arraySwiper[main_control].slidePrev();
                                            }
                                        } else {
                                            while (parentController) {
                                                if (wbps_arraySwiper[parentController].isBeginning) {
                                                    // 到达父容器的最前或最后，追溯更上一级父容器
                                                    parentController = $('.' + parentController).parent().parent().parent().attr("controller");

                                                    if (slideObjectController.parent().parent().parent().hasClass('wbht_slider_detail')) {
                                                        var main_control = 'main_control';
                                                        wbps_arraySwiper[main_control].slidePrev();
                                                    }
                                                } else {
                                                    wbps_arraySwiper[parentController].slidePrev();
                                                    parentController = null;
                                                }
                                            }
                                        }
                                    }
                                }
                            })



                        } else if ('next' == swiper.swipeDirection) {

                            // while (parentController) {
                            //     if (wbps_arraySwiper[parentController].isEnd) {
                            //         parentController = $('.' + parentController).parent().parent().parent().attr("controller");
                            //     } else {
                            //         wbps_arraySwiper[parentController].slideNext();
                            //         parentController = null;
                            //     }
                            // }

                            $('.wbps_slider').each(function () {

                                if ($(this).attr('controller') == 'main_control') {

                                    // 户型鉴赏:特殊处理
                                    var main_control_initindex = $(this).attr('initindex');
                                    // 总览在下
                                    if (main_control_initindex == '1') {

                                        while (parentController) {
                                            if (wbps_arraySwiper[parentController].isEnd) {
                                                // 到达父容器的最前或最后，追溯更上一级父容器
                                                parentController = $('.' + parentController).parent().parent().parent().attr("controller");
                                                if (slideObjectController.parent().parent().parent().hasClass('wbht_slider_detail')) {
                                                    var main_control = 'main_control';
                                                    wbps_arraySwiper[main_control].slideNext();
                                                }
                                            } else {
                                                wbps_arraySwiper[parentController].slideNext();
                                                parentController = null;
                                            }
                                        }
                                    } else {
                                        while (parentController) {
                                            if (wbps_arraySwiper[parentController].isEnd) {
                                                parentController = $('.' + parentController).parent().parent().parent().attr("controller");
                                            } else {
                                                wbps_arraySwiper[parentController].slideNext();
                                                parentController = null;
                                            }
                                        }
                                    }
                                }
                            })
                        }
                    }
                },
                onInit: function (swiper) {  //Swiper2.x的初始化是onFirstInit
                    // swiperAnimateCache(swiper);  //隐藏动画元素
                    // swiperAnimate(swiper); //初始化完成开始动画
                    // 暂时屏蔽,只有第一个有ani元素的滑动器才能执行swiperAnimate
                    if (firstAnimate && this_swiper.children('.swiper-wrapper').children('.swiper-slide').children('.wbps_item_image.ani').length > 0) {
                        swiperAnimate(swiper); //初始化完成开始动画
                        firstAnimate = false;
                    }
                },
                onSlideChangeEnd: function (swiper) {
                    try {
                        swiperAnimate(swiper); //每个slide切换结束时也运行当前slide动画
                    } catch (e) {
                    }
                }
            });
        }
    });

    // 加入根据参数跳转滑动起功能，参数格式:param={controller},{index},{controller},{index}...
    var param = wbps_GetUrlParam(window.location.href, 'param');
    if (param && param.length > 0 && param.indexOf(",") > 0) {
        var paramSplit = param.split(',');
        for (var index = 0; index < paramSplit.length; index += 2) {
            var sliderController = paramSplit[index];
            var sliderIndex = paramSplit[index + 1];
            $(".wbps_slider_menu").each(function () {
                if (sliderController == $(this).attr('controller')) {
                    $(this).children('img').each(function (index) {
                        if (index == sliderIndex) {
                            wbps_updateMenuStatu($(this));
                        };
                    });
                }
            });
            wbps_arraySwiper[sliderController].slideTo(sliderIndex, 0, true);
        }
    }

    // 菜单切换
    $(".wbps_slider_menu>img").click(function () {
        //
        wbps_updateMenuStatu($(this));
        //
        var controller = $(this).parent().attr("controller");
        wbps_arraySwiper[controller].slideTo($(this).index(), 1000, true);

        // 户型详情和总览的切换处理
        var curSliderIndex = $(this).index();
        var curContainer = wbps_arraySwiper[controller].container;
        wbht_syncUpdateSlider(curContainer, curSliderIndex);
    });

    // 初始化时自动切换到高亮菜单对应的模块
    $(".wbps_slider_menu>img").each(function () {
        if ($(this).hasClass('action')) {
            var index = $(this).index();
            var controller = $(this).parent().attr("controller");
            try {
                wbps_arraySwiper[controller].slideTo(index, 1000, false);
                // 户型详情和总览的切换处理
                var curContainer = wbps_arraySwiper[controller].container;
                wbht_syncUpdateSlider(curContainer, index);
            }
            catch (e) { }
        }
    });


    // 点击按钮弹出图
    $('.wbps_item_button').click(function () {
        //
        var showsrc = $(this).attr('showsrc');
        if (showsrc) {
            if ($(this).attr('showname')) {
                var showname = $(this).attr('showname');
                $('.' + showname).remove();
                $(this).attr('showname', null);
                if ($(this).hasClass('wbps_autozoom')) {
                    $(this).removeClass('wbps_animation_zoom');
                }
            } else {
                // 生成唯一的class名，便于重新定位删除
                var showname = 'show_' + new Date().getTime();
                // 附加按钮参数
                $(this).attr('showname', showname);
                if ($(this).hasClass('wbps_autozoom')) {
                    $(this).addClass('wbps_animation_zoom');
                }
                // 显示图
                var showstyle = $(this).attr('showstyle');
                var showclass = $(this).attr('showclass');
                $(this).parent().append('<img class="wbps_item_button_show ' + showname + ' ' + showclass + '" src="' + showsrc + '" style="' + showstyle + '">');
                // 点击弹出图直接隐藏删除
                $('.wbps_item_button_show').click(function () {
                    //
                    $(this).remove();
                    //
                    var curObjectImage = $(this);
                    $('.wbps_item_button').each(function () {
                        if (curObjectImage.hasClass($(this).attr('showname'))) {
                            $(this).attr('showname', null);
                            if ($(this).hasClass('wbps_autozoom')) {
                                $(this).removeClass('wbps_animation_zoom');
                            }
                        }
                    });
                });
            }
        }
    });
    $('.wbps_item_button_single').click(function () {
        // 每次只显示一个，故要先清楚其他同类状态
        $(this).siblings('.wbps_item_button_single_show').remove();
        if ($(this).hasClass('wbps_autozoom')) {
            $('.wbps_item_button_single').removeClass('wbps_animation_zoom');
        }

        var showsrc = $(this).attr('showsrc');
        if (showsrc) {
            if ($(this).attr('showname')) {
                var showname = $(this).attr('showname');
                $('.' + showname).remove();
                $(this).attr('showname', null);
            } else {
                $('.wbps_item_button_single').attr('showname', null);

                var showname = 'wbps_item_button_single_show';
                // 附加按钮参数
                $(this).attr('showname', showname);
                if ($(this).hasClass('wbps_autozoom')) {
                    $(this).addClass('wbps_animation_zoom');
                }
                // 显示图
                var showstyle = $(this).attr('showstyle');
                var showclass = $(this).attr('showclass');
                $(this).parent().append('<img class="wbps_item_button_show ' + showname + ' ' + showclass + '" src="' + showsrc + '" style="' + showstyle + '">');
                // 点击弹出图直接隐藏删除
                $('.wbps_item_button_show').click(function () {
                    //
                    $(this).remove();
                    //
                    var curObjectImage = $(this);
                    $('.wbps_item_button_single').each(function () {
                        if (curObjectImage.hasClass($(this).attr('showname'))) {
                            $(this).attr('showname', null);
                            if ($(this).hasClass('wbps_autozoom')) {
                                $(this).removeClass('wbps_animation_zoom');
                            }
                        }
                    });
                });
            }
        }
    });

    /***************************************************************************
     * 辅助函数
     * TODO：此高亮逻辑有点弱，必须要求图片同名加_h作为高亮
     */
    // 更新按钮菜单状态
    function wbps_updateMenuStatu(curMenu) {
        // 清除旧状态
        var oldActionMenu = curMenu.parent().find('.action');
        oldActionMenu.attr('src', oldActionMenu.attr('src').replace('_h.', '.'));

        var style = oldActionMenu.attr('style');
        oldActionMenu.attr('style', oldActionMenu.attr('h_style'));
        oldActionMenu.attr('h_style', style);

        //更新Action
        curMenu.addClass("action").siblings().removeClass("action");
        // 添加新高亮
        curMenu.attr('src', curMenu.attr('src').replaceAll ('.png', '_h.png'));

        var style = curMenu.attr('style');
        curMenu.attr('style', curMenu.attr('h_style'));
        curMenu.attr('h_style', style);
    }

    function wbps_GetUrlParam(url, arg) {
        if ('#' == url[url.length - 1]) {
            url = url.substr(0, url.length - 1);
        }

        var urlSplit = url.split("?");
        if (urlSplit.length > 1) {
            var reg = new RegExp("(^|&)" + arg + "=([^&]*)(&|$)", "i");
            var result = urlSplit[1].match(reg);
            if (result != null) {
                return unescape(result[2]);
            }
        }
        return null;
    }


    // 更新按钮菜单状态
    function wbht_updateSliderMenuAction(controller, showIndex) {
        $(".wbps_slider_menu").each(function () {
            if (controller == $(this).attr('controller')) {
                $(this).children('img').each(function (index) {
                    if (index == showIndex) {
                        wbps_updateMenuStatu($(this));
                    };
                });
            }
        });
    }

    // 根据controller创建滑动器
    function wbht_buildSlider(objectSlider, paramSlider) {
        var controller = objectSlider.attr("controller");
        if (controller) {
            objectSlider.addClass(controller);
            var objectController = "." + controller;
            wbps_arraySwiper[controller] = new Swiper(objectController, paramSlider);
        }
    }


    // 户型特别处理：总览和详情分别切换到对应的
    function wbht_syncUpdateSlider(curContainer, curSliderIndex) {
        if (curContainer.hasClass("wbht_slider_overview")) {
            // 滑动详情到对应户型
            var detailContainer = curContainer.parent().parent().find('.wbht_slider_detail');
            if (detailContainer && detailContainer.length > 0) {
                var controller = detailContainer.attr('controller');
                wbps_arraySwiper[controller].slideTo(curSliderIndex, 0, false);
                wbht_updateSliderMenuAction(controller, curSliderIndex);
            }
        } else if (curContainer.hasClass("wbht_slider_detail")) {
            // 详情切换户型容器
            var overviewContainer = curContainer.parent().parent().find('.wbht_slider_overview');
            if (overviewContainer && overviewContainer.length > 0) {
                var controller = overviewContainer.attr('controller');
                wbps_arraySwiper[controller].slideTo(curSliderIndex, 0, false);
                wbht_updateSliderMenuAction(controller, curSliderIndex);
            }
        } else {
            //增强总览和详情有多级菜单的对应关系（除了总览和详细的名字外，其他文件夹的名字要能对应上才有效）
            var curController = curContainer.attr("controller");
            if (curController != null) {
                var matchContainer = '';
                if (curController.indexOf("xiangqing") > -1) {
                    matchContainer = curController.replace(/xiangqing/g, 'zonglan');

                } else if (curController.indexOf("zonglan") > -1) {
                    matchContainer = curController.replace(/zonglan/g, 'xiangqing')
                }

                if (wbps_arraySwiper[matchContainer]) {
                    wbps_arraySwiper[matchContainer].slideTo(curSliderIndex, 0, false);
                    wbht_updateSliderMenuAction(matchContainer, curSliderIndex);
                }
            }
        }
    }

    // 自动显示点击交互的详情
    $('.wbps_default_show_detail').trigger("click");
});
